using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
 
public class HttpClientHelper
{
    #region 字段
 
    private static HttpClient HttpClientWrapper = new HttpClient();
 
    private static int TimeOut = 3;
 
    #endregion
 
    #region Get
 
    /// <summary>
    /// Get 请求
    /// </summary>
    /// <param name="url">链接</param>
    /// <returns></returns>
    /// <exception cref="TimeoutException"></exception>
    /// <exception cref="Exception"></exception>
    public static async Task<string> Get(string url)
    {
        var response = await HttpClientWrapper.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
 
    /// <summary>
    /// Get 请求（注意 headerValue 只能单行，不能加入换行符等特殊字符）
    /// </summary>
    /// <param name="url">链接</param>
    /// <param name="headerKey">标题key</param>
    /// <param name="headerValue">标题value</param>
    /// <returns></returns>
    /// <exception cref="TimeoutException"></exception>
    /// <exception cref="Exception"></exception>
    public static async Task<string> Get(string url, string headerKey, string headerValue)
    {
        HttpClientWrapper.DefaultRequestHeaders.Clear();
        HttpClientWrapper.DefaultRequestHeaders.Add(headerKey, headerValue);
 
        var response = await HttpClientWrapper.GetAsync(url);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
 
    #endregion
 
    #region Post
 
    /// <summary>
    /// Post 请求
    /// </summary>
    /// <param name="url">链接</param>
    /// <param name="content">发送内容</param>
    /// <returns></returns>
    public static async Task<string> Post(string url, string content)
    {
        var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
        var response = await HttpClientWrapper.PostAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
    public static async Task<string> Post(string url, string content,bool import)
    {
        var httpContent = new StringContent(content, Encoding.UTF8, "application/octet-stream");
        var response = await HttpClientWrapper.PostAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
    /// <summary>
    /// Post 请求（注意 headerValue 只能单行，不能加入换行符等特殊字符）
    /// </summary>
    /// <param name="url">链接</param>
    /// <param name="content">发送内容</param>
    /// <param name="headerKey">标题key</param>
    /// <param name="headerValue">标题value</param>
    /// <returns></returns>
    /// <exception cref="TimeoutException"></exception>
    /// <exception cref="Exception"></exception>
    public static async Task<string> Post(string url, string content, string headerKey, string headerValue)
    {
        HttpClientWrapper.DefaultRequestHeaders.Clear();
        HttpClientWrapper.DefaultRequestHeaders.Add(headerKey, headerValue);
 
        var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
        var response = await HttpClientWrapper.PostAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
    
    /// <summary>
    /// Post 请求（注意 headers 参数只能单行，不能加入换行符等特殊字符）
    /// </summary>
    /// <param name="url">链接</param>
    /// <param name="headers">标题</param>
    /// <param name="content">发送内容</param>
    /// <returns></returns>
    /// <exception cref="TimeoutException"></exception>
    /// <exception cref="Exception"></exception>
    public static async Task<string> Post(string url, string content, Dictionary<string, string> headers)
    {
        HttpClientWrapper.DefaultRequestHeaders.Clear();
        foreach (var header in headers)
            HttpClientWrapper.DefaultRequestHeaders.Add(header.Key, header.Value);
 
        var httpContent = new StringContent(content, Encoding.UTF8, "application/json");
        var response = await HttpClientWrapper.PostAsync(url, httpContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
 
    /// <summary>
    /// Post 请求（注意 headers 参数只能单行，不能加入换行符等特殊字符）
    /// </summary>
    /// <param name="url"></param>
    /// <param name="parameters">参数合集</param>
    /// <param name="headers"></param>
    /// <returns></returns>
    /// <exception cref="TimeoutException"></exception>
    /// <exception cref="Exception"></exception>
    public static async Task<string> Post(string url, Dictionary<string, string> parameters, Dictionary<string, string> headers)
    {
        HttpClientWrapper.DefaultRequestHeaders.Clear();
        foreach (var header in headers)
            HttpClientWrapper.DefaultRequestHeaders.Add(header.Key, header.Value);
 
        var formContent = new FormUrlEncodedContent(parameters);
        var response = await HttpClientWrapper.PostAsync(url, formContent);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
 
    #endregion
 
    #region 构造函数
 
    static HttpClientHelper()
    {
        HttpClientWrapper.Timeout = TimeSpan.FromSeconds(TimeOut);
    }
 
    private HttpClientHelper() { }
 
    #endregion
}